#include <modelConf.h>

namespace Model {

  ModelConf *ModelConf::instance = NULL;

  bool ModelConf::ReadConfFileItem(std::ifstream &in,std::string path)
  {
    std::string line;
    std::string filePath = path + "ModelDouble.conf";
    in.open(filePath.c_str());
    if(!in){
      std::cout<<"wrong\n";
      return false;
    }
    while(getline(in,line)){
      if(' ' == line[0] || '#' == line[0] || 0 == line.size() )
	continue;
      size_t pos = line.find(" ");
      std::string name = line.substr(0,pos);
      line = line.substr(pos + 1);
      double size = atoi(line.c_str());
      doubleData.insert( std::pair<std::string,double>(name,size) );
      //      std::cout<<"[ "<<name<<" ] = "<<size<<"\n";
    }
    in.close();
    filePath = path + "ModelString.conf";
    in.open(filePath.c_str());
    if(!in){
      std::cout<<"wrong2\n";
      return false;
    }
    while(getline(in,line)){
      if(' ' == line[0] || '#' == line[0] || 0 == line.size() )
        continue;
      size_t pos = line.find(" ");
      std::string name = line.substr(0,pos);
      line = line.substr(pos + 1);
      stringData.insert( std::pair<std::string,std::string>(name,line) );
      //      std::cout<<"[ "<<name<<" ] = "<<line<<"\n";
    }
    in.close();
    return true;
  }

  bool ModelConf::ReadConfFile()
  {
    std::ifstream in;
    std::string line,path;
    path = MODEL_CONF_PATH + static_cast<std::string>("msg/");
    std::cout<<"now read the default conf file [ "<<path<<" ]\n";
    ReadConfFileItem(in,path);
    return false;
  }

  ModelConf::ModelConf()
  {
    ReadConfFile();
  }

  ModelConf *ModelConf::GetModelConf()
  {
    if( NULL == ModelConf::instance)
      ModelConf::instance = new ModelConf();
    return ModelConf::instance;
  }

  ModelConf::~ModelConf()
  {

  }

  double ModelConf::GetDoubleData(const std::string &name)
  {
    return doubleData.find(name)->second;
  }

  std::string ModelConf::GetStringData(const std::string &name)
  {
    return stringData.find(name)->second;
  }

}
